﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GoldPrinter;
using System.Management;
using System.Drawing.Printing;
namespace BYManageInfoSystem
{
    public partial class Frm_PrinterSetting : Form
    {
        public static DGBY.BLL.TaoDaPrinterNameTab bllTaoDaPrinterNameTab = new DGBY.BLL.TaoDaPrinterNameTab();
        public Frm_PrinterSetting()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            PrintDocument printDoc = new PrintDocument();
            string strPrinterName = printDoc.PrinterSettings.PrinterName;


            PrintDialog dialog = new PrintDialog();
            string strTemp = "";
            if (dialog.ShowDialog(null) == DialogResult.OK)
                strTemp = dialog.PrinterSettings.PrinterName;
            ManagementObjectSearcher query;
            ManagementObjectCollection queryCollection;
            string _classname = "SELECT * FROM Win32_Printer";

            query = new ManagementObjectSearcher(_classname);
            queryCollection = query.Get();

            foreach (ManagementObject mo in queryCollection)
            {
                if (string.Compare(mo["Name"].ToString(), strTemp, true) == 0)
                {
                    mo.InvokeMethod("SetDefaultPrinter", null);
                    break;
                }
            }
            ExcelBase eB = new ExcelBase();
            string strPrinterNameWithPort = eB.GetActivePrinterNameWithPort();
            eB.Close();

            DataSet ds = bllTaoDaPrinterNameTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                int nId = Convert.ToInt32(ds.Tables[0].Rows[0]["TaoDaPrinterNameTabID"].ToString(), 10);
                string strPrinterNameTemp = ds.Tables[0].Rows[0]["TaoDaPrinterName"].ToString();
                DGBY.Model.TaoDaPrinterNameTab modelTaoDaPrinterNameTab = new DGBY.Model.TaoDaPrinterNameTab();
                modelTaoDaPrinterNameTab.TaoDaPrinterNameTabID = nId;
                modelTaoDaPrinterNameTab.TaoDaPrinterName = strPrinterNameWithPort;
                bllTaoDaPrinterNameTab.Update(modelTaoDaPrinterNameTab);
            }
            else
            {
                DGBY.Model.TaoDaPrinterNameTab modelTaoDaPrinterNameTab = new DGBY.Model.TaoDaPrinterNameTab();
                modelTaoDaPrinterNameTab.TaoDaPrinterName = strPrinterNameWithPort;
                bllTaoDaPrinterNameTab.Add(modelTaoDaPrinterNameTab);
            }
            foreach (ManagementObject mo in queryCollection)
            {
                if (string.Compare(mo["Name"].ToString(), strPrinterName, true) == 0)
                {
                    mo.InvokeMethod("SetDefaultPrinter", null);
                    break;
                }
            }

            this.textBox1.Text = strPrinterNameWithPort;
        }

        private void Frm_PrinterSetting_Load(object sender, EventArgs e)
        {
            DataSet ds = bllTaoDaPrinterNameTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                string strPrinterNameTemp = ds.Tables[0].Rows[0]["TaoDaPrinterName"].ToString();
                this.textBox1.Text = strPrinterNameTemp;
            }

        }
    }
}
